//
//  HMDBManager.swift
//  WeiBo
//
//  Created by apple on 16/7/5.
//  Copyright © 2016年 itheima. All rights reserved.
//

import Foundation
import FMDB

class HMDBManager {
    
    static let shareInstance: HMDBManager = HMDBManager()
    
    // 声明一个fmdatabasequeue
    let queue: FMDatabaseQueue
    
    private init(){
        
        // 1. 设置文件路径
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("hmweibo.db")
        
        // 2. 初始化 queue
        queue = FMDatabaseQueue(path: path)
        
        // 3. 创建表格
        
        createTabel()
    }
    
    private func createTabel(){
        
        let sql = "CREATE TABLE IF NOT EXISTS 'T_Status' (" +
        "'status' text," +
        "'statusId' integer NOT NULL," +
        "'userId' integer," +
        "PRIMARY KEY('statusId')" +
        ");"
        
        // 打印sql
        print(sql)
        
        queue.inDatabase { (db) -> Void in
            if db.executeUpdate(sql, withArgumentsInArray: nil) {
                print("创建表格成功")
            }
        }
        
    }
    
}